#include "Material.h"
#include "GL/gl.h"

#define MATERIAL_WHITE { 0.0f, 0.0f, 0.0f, 0.0f }

Material::Material() : ambient(MATERIAL_WHITE), diffuse(MATERIAL_WHITE),
					   specular(MATERIAL_WHITE), emission(MATERIAL_WHITE),
					   shininess(100.0f), enableAmbient(false),
					   enableDiffuse(false), enableSpecular(false),
					   enableEmission(false), enableShininess(false) {
}

#undef MATERIAL_WHITE

Material::~Material() {
}

void Material::enable(int flag) {
	switch(flag) {
		case GL_AMBIENT:
			this->enableAmbient = true;
			break;
		case GL_DIFFUSE:
			this->enableDiffuse = true;
			break;
		case GL_AMBIENT_AND_DIFFUSE:
			this->enableAmbient = true;
			this->enableDiffuse = true;
			break;
		case GL_SPECULAR:
			this->enableSpecular = true;
			break;
		case GL_EMISSION:
			this->enableEmission = true;
			break;
		case GL_SHININESS:
			this->enableShininess = true;
			break;
		default:
			break;
	}
}

void Material::disable(int flag) {
	switch(flag) {
		case GL_AMBIENT:
			this->enableAmbient = false;
			break;
		case GL_DIFFUSE:
			this->enableDiffuse = false;
			break;
		case GL_AMBIENT_AND_DIFFUSE:
			this->enableAmbient = false;
			this->enableDiffuse = false;
			break;
		case GL_SPECULAR:
			this->enableSpecular = false;
			break;
		case GL_EMISSION:
			this->enableEmission = false;
			break;
		case GL_SHININESS:
			this->enableShininess = false;
			break;
		default:
			break;
	}
}

void Material::setAmbient(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha) {
	this->enableAmbient = true;
	this->ambient[0] = red;
	this->ambient[1] = green;
	this->ambient[2] = blue;
	this->ambient[3] = alpha;
}

void Material::setDiffuse(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha) {
	this->enableDiffuse = true;
	this->diffuse[0] = red;
	this->diffuse[1] = green;
	this->diffuse[2] = blue;
	this->diffuse[3] = alpha;
}

void Material::setSpecular(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha) {
	this->enableSpecular = true;
	this->specular[0] = red;
	this->specular[1] = green;
	this->specular[2] = blue;
	this->specular[3] = alpha;
}

void Material::setEmission(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha) {
	this->enableEmission = true;
	this->emission[0] = red;
	this->emission[1] = green;
	this->emission[2] = blue;
	this->emission[3] = alpha;
}

void Material::setShininess(GLfloat shininess) {
	this->enableShininess = true;
	this->shininess = shininess;
}

void Material::render() const {
	if(this->enableAmbient) {
		glMaterialfv(GL_FRONT, GL_AMBIENT, this->ambient);
	}
	if(this->enableDiffuse) {
		glMaterialfv(GL_FRONT, GL_AMBIENT, this->diffuse);
	}
	if(this->enableSpecular) {
		glMaterialfv(GL_FRONT, GL_AMBIENT, this->specular);
	}
	if(this->enableEmission) {
		glMaterialfv(GL_FRONT, GL_AMBIENT, this->emission);
	}
	if(this->enableShininess) {
		glMaterialf(GL_FRONT, GL_AMBIENT, this->shininess);
	}
}
